﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class ThemPhuKienDT : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    KetNoi obj = new KetNoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label3.Visible = false;
            LoadLoaiPhuKien();
            Calendar1.SelectedDate = DateTime.Today;

        }
    }
    private void luuhinh(FileUpload a)// Luu Hinh
    {
        string strfilename;
        if (a.FileName != "")
        {
            strfilename = Server.MapPath("Upload\\" + a.FileName);
            a.PostedFile.SaveAs(strfilename);
        }
    }
    protected void XoaText()
    {

        txttenpk.Text = "";
        txtgia.Text = "";
        txtchucnang.Text = "";
        txtbaohanh.Text = "";
        txtthietbisudung.Text = "";
        txtthongtin.Text = "";
        txtxuatxu.Text = "";
       
    }
    protected void LoadLoaiPhuKien()
    {
        dt = obj.loadDataTable("Select * from LoaiPhuKien");
        drploaipk.DataTextField = "TenLoaiPK";
        drploaipk.DataValueField = "MaLoaiPK";
        drploaipk.DataSource = dt;
        drploaipk.DataBind();
    }
    protected void btnluu_Click(object sender, EventArgs e)
    {
          int n = 0;
          if (int.TryParse(this.txtgia.Text, out n))// Cho phep nhap so
          {
              luuhinh(FileUploadHinh);
              string tenpk = txttenpk.Text.ToString();
              string gia = txtgia.Text.ToString();
              string thietbisudung = txtthietbisudung.Text.ToString();
              string chucnang = txtchucnang.Text.ToString();
              string xuatxu = txtxuatxu.Text.ToString();
              string baohanh = txtbaohanh.Text.ToString();
              DateTime ngaydang = Calendar1.SelectedDate;
              string thongtinchitiet = txtthongtin.Text.ToString();
              string hinhanh = FileUploadHinh.FileName.ToString();
              string maloaiphukien = drploaipk.SelectedValue.ToString();
              obj.ThemPhuKien(tenpk, gia, thietbisudung, chucnang, xuatxu, baohanh, ngaydang, thongtinchitiet, hinhanh, maloaiphukien);
              string mess = "Bạn đã lưu thành công";
              string strBuilder = "<script language='javascript'>alert('" + mess + "')</script>";
              Response.Write(strBuilder);
              XoaText();
          }
          else
          {
              Label3.Visible = true;
              Label3.Text = "Gia chi duoc nhap so";
          }
    }
}
